package com.xiaoyu.base.utils.extensions

import android.view.View
import com.xiaoyu.base.view.OnClickDebounceListener

private const val CP_VIEW_TAG = 30 shl 24

fun View.setOnClickDebounceListener(onClickListener: View.OnClickListener) {
    this.setOnClickListener(object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            onClickListener.onClick(v)
        }
    })
}

fun View.setOnClickDebounceListener(listener: (View) -> Unit) {
    this.setOnClickListener(object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            listener.invoke(v)
        }
    })
}


fun <T> View?.getViewTag(): T? {
    val tag = this?.getTag(CP_VIEW_TAG)
    return tag as? T
}

fun View?.setViewTag(any: Any?) {
    this?.setTag(CP_VIEW_TAG, any)
}

fun <T> View?.getViewTag(key: Int): T? {
    var key = key
    key = 30 + key shl 24

    return this?.getTag(key) as? T
}

fun View?.setViewTag(key: Int, any: Any?) {
    var key = key
    key = 30 + key shl 24

    this?.setTag(key, any)
}
